package http

import (
	"net/http"

	"github.com/gin-gonic/gin"
	men "github.com/liyaozong/mengo"

	"github.com/liyaozong/mengo/common/di"
	"github.com/liyaozong/mengo/core/base/container"

	commonDTO "github.com/liyaozong/mengo/contracts/dtos/common"
)

// CommonController controller for REST APIs
type CommonController struct {
	dic         *di.Container
	serviceName string
}

// NewCommonController creates and initializes an CommonController
func NewCommonController(dic *di.Container, serviceName string) *CommonController {
	return &CommonController{
		dic:         dic,
		serviceName: serviceName,
	}
}

// Ping handles the request to /ping endpoint. Is used to test if the service is working
// It returns a response as specified by the API swagger in the openapi directory
func (c *CommonController) Ping(ctx *gin.Context) {
	response := commonDTO.NewPingResponse(c.serviceName)
	ctx.JSON(http.StatusOK, response)
}

// Version handles the request to /version endpoint. Is used to request the service's versions
// It returns a response as specified by the API swagger in the openapi directory
func (c *CommonController) Version(ctx *gin.Context) {
	response := commonDTO.NewVersionResponse(men.Version, c.serviceName)
	ctx.JSON(http.StatusOK, response)
}

// Config handles the request to /config endpoint. Is used to request the service's configuration
// It returns a response as specified by the API swagger in the openapi directory
func (c *CommonController) Config(ctx *gin.Context) {
	response := commonDTO.NewConfigResponse(container.ConfigurationFrom(c.dic.Get), c.serviceName)
	ctx.JSON(http.StatusOK, response)
}
